package com.example.filesystem.controller;

import com.example.filesystem.model.User;
import com.example.filesystem.service.AuthService;
import com.example.filesystem.util.AuthUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/filetrans/admin")
public class AdminController {
    private final AuthService authService;

    public AdminController(AuthService authService) {
        this.authService = authService;
    }

    @GetMapping("/users")
    public String userManagement(Model model) {
        // 获取所有用户
        String username = AuthUtil.currentUsername();
        if (!"admin".equals(username)) {
            return "redirect:/filetrans/login";
        }
        List<User> users = authService.getAllUsers();
        model.addAttribute("users", users);
        return "users";
    }

    @PostMapping("/users/update")
    public ResponseEntity<String> update(@RequestBody User user) {
        authService.update(user.getId(), user.getPassword());
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("/users/{userId}")
    public ResponseEntity<String> userExist(@PathVariable Long userId) {
        authService.deleteById(userId);
        return ResponseEntity.ok("ok");
    }
}